package com.cskaoyan.market.controller;


import com.cskaoyan.market.db.domain.MarketOrderGoods;
import com.cskaoyan.market.service.WxOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.OrderListVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @Autowired
    SecurityManager securityManager;

    @GetMapping("list")
    public Object getOrderGoodsList(Integer showType,Integer page,Integer limit){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer id = (Integer) session.getAttribute("id");

        List<OrderListVo> orderList = wxOrderService.getOrderList(showType,page,limit,id);
        return ResponseUtil.okList(orderList);
    }

    @GetMapping("detail")
    public Object getOrderListDetail(Integer orderId){
        Map<String, Object> orderDetailList = wxOrderService.getOrderDetailList(orderId);
        return ResponseUtil.ok(orderDetailList);
    }

    @PostMapping("cancel")
    public Object orderCancel(@RequestBody MarketOrderGoods marketOrderGoods){
        wxOrderService.orderCancel(marketOrderGoods.getOrderId());
        return ResponseUtil.ok();
    }

    @PostMapping("submit")
    public Object orderSubmit(@RequestBody Map<String,Object> map){
        wxOrderService.orderSubmit(map);
        return ResponseUtil.ok();
    }

    @PostMapping("comment")
    public Object orderCommit(@RequestBody Map<String,Object> map){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer id = (Integer) session.getAttribute("id");
        wxOrderService.orderComment(map,id);
        return ResponseUtil.ok();
    }

    @PostMapping("refund")
    public Object orderRefund(@RequestBody MarketOrderGoods marketOrderGoods){
        wxOrderService.orderRefund(marketOrderGoods.getOrderId());
        return ResponseUtil.ok();
    }

    @PostMapping("delete")
    public Object orderDelete(@RequestBody MarketOrderGoods marketOrderGoods){
        wxOrderService.orderDelete(marketOrderGoods.getOrderId());
        return ResponseUtil.ok();
    }

    @PostMapping("confirm")
    public Object orderComfirm(@RequestBody MarketOrderGoods marketOrderGoods){
        wxOrderService.orderConfirm(marketOrderGoods.getOrderId());
        return ResponseUtil.ok();
    }

    @GetMapping("goods")
    public Object orderGoods(Integer goodsId){
        MarketOrderGoods marketOrderGoods = wxOrderService.orderGoods(goodsId);
        return ResponseUtil.ok(marketOrderGoods);
    }
}

